var app = getApp();
var ddapi = require("../../../utils/md5.js");
Page({
  data: {
    orderId: 0,
    ordernum:'',
    goodsList: [],
    orderDetail:[],
    yunPrice: "10.00",
    statusSteps: [
      {
        current: false,
        done: false,
        text: '待支付',
        desc: ''
      },
      {
        done: false,
        current: false,
        text: '待使用',
        desc: ''
      },
      {
        done: false,
        current: false,
        text: '待评价',
        desc: ''
      },
      {
        done: false,
        current: false,
        text: '已完成',
        desc: ''
      }
    ],

    controls: [
      {
        id: 'sys_1',
        iconPath: '/images/nav/map.png',
        position: {
          left: 325,
          top: 60 - 30,
          width: 40,
          height: 40
        },
        clickable: true
      }, 
    ],
    height: '0px',
    is_map: false,
    is_distribution:true,
    is_callqishou:true
  },

  onLoad: function (e) {
  		var orderId = e.id;
      var ordernum = e.ordernum;
  		this.data.orderId = orderId;
  		this.setData({
  			orderId: orderId,
  			userTel: app.globalData.userTel,
        ordernum:ordernum,
  			orderState: wx.getStorageSync('orderState'),
  		});
      
      var that = this;
      var open_id = wx.getStorageSync('openid');
      var order_id = that.data.orderId;
      var service = 'User.GetNewOrderInfo';
      var user_id = wx.getStorageSync('user_id');
      var sign = ddapi.md5(open_id + order_id + service + user_id);
      wx.request({
        url: app.globalData.apiUrl + '?service=User.GetNewOrderInfo',
        header: {
          'Content-Type': 'json'
        },
        data: {
          sign: sign,
          user_id: user_id,
          open_id: open_id,
          order_id: order_id,
        },
        success: function (res) {
          console.log(res);
          var result = res.data.data;
          if (result.code == 0) {
            that.setData({
              ordernum: result.info[0].order_num,
              orderDetail: result.info
            });
            if (result.info[0].delive_type == 2 && result.info[0].order_pid != 1){
              console.log(result.info[0].deliveryNo);
              if (result.info[0].deliveryNo != ""){
                that.getDadaQuery(result.info[0])
              }else{
                that.setData({
                  is_map: true,
                  is_distribution: false,
                  distribution_tel: result.info[0].order_shop_tel,
                  distribution_name: '联系商家',
                  distribution_service:'商家自配',
                  distribution_time: '立即配送',
                  distribution_address: result.info[0].delive_address
                })
              }             
            }else{
              that.setData({
                is_map: true,
              })
            }
            that.updateStatusSteps();
          } else {
            wx.hideLoading();
            that.setData({
              orderList: 'null',
            });
            wx.showModal({
              title: '失败',
              content: result.msg,
              showCancel: false
            })
            return;
          }
        },
        complete: function (res) {
          console.log(res);
        },
      })
  	},
  
  //用于获取配送信息查询
  getDadaQuery: function (info){
    var that = this;
    var service = 'Dada.Query'
    var order_id = info.order_num
    var delive_address = info.delive_address
    var jingwei = info.delive_jingwei.split(",")
    var shop_jingwei = info.shop_address_jingwei.split(",")
    var lat = jingwei[0];
    var lng = jingwei[1];
    var shop_lat = shop_jingwei[0];
    var shop_lng = shop_jingwei[1];
    var sign = ddapi.md5(order_id + service);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        order_id: order_id,
      },
      success: function (res) {
        console.log(res);
        var result = res.data.data;
        if (result.code == 0){
          that.setData({
            height: '300px',
            is_map: false,
            is_distribution: false,
            is_callqishou:false,
            distribution_tel: result.info.supplierPhone,
            distribution_name: '联系骑手',
            distribution_service: '达达配送',
            distribution_time: '立即配送',
            distribution_address: delive_address,
            latitude: lat,
            longitude: lng,
            markers: [{
              id: 1,
              latitude: lat,
              longitude: lng,

            },
            {
              id: 2,
              latitude: result.info.supplierLat,
              longitude: result.info.supplierLng,
              iconPath: '/images/nav/qis.png',
              width: 20,
              height: 20,
              label: {
                content: result.info.statusMsg,
                fontSize: 13,
                color: '#e62a39',
                anchorX: 1,
                anchorY: 5,
                bgColor: '#ffffff',
                textAlign: 'center',
                padding: 4,
                borderRadius: 6,
                borderWidth: 1,
                borderColor: "#e62a39",
                display: 'ALWAYS',
              },
            },
              {
                id: 3,
                latitude: shop_lat,
                longitude: shop_lng,
                iconPath: '/images/nav/shop2.png',
                width: 20,
                height: 20,
              }
            ],
          })
        }else{
          that.setData({
            height: '300px',
            is_map: false,
            is_distribution: false,
            distribution_tel: info.order_shop_tel,
            distribution_name: '联系商家',
            distribution_service: '达达配送',
            distribution_time: '立即配送',
            distribution_address: delive_address,
            latitude: lat,
            longitude: lng,
            markers: [{
              id: 1,
              latitude: lat,
              longitude: lng,
            },
            {
              id: 2,
              latitude: shop_lat,
              longitude: shop_lng,
              iconPath: '/images/nav/shop2.png',
              width: 20,
              height: 20,
              label: {
                content: '待接单',
                fontSize: 13,
                color: '#e62a39',
                anchorX: 1,
                anchorY: 5,
                bgColor: '#ffffff',
                textAlign: 'center',
                padding: 4,
                borderRadius: 6,
                borderWidth: 1,
                borderColor: "#e62a39",
                display: 'ALWAYS',
              },
            }
            ],
          })
        }
        that.controltap();
      },
      complete: function (res) {
        console.log(res);
      },
      fail: function () {
        //请求超时提示
        wx.showModal({
          title: '提示',
          content: '网络超时',
          showCancel: false
        })
        return;
      }
    })
  },


  wuliuDetailsTap: function (e) {
    var orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/wuliu/index?id=" + orderId
    })
  },
  confirmBtnTap: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认您已收到商品？',
      content: '',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            url: 'https://ml002.zntech-china.com/' + app.globalData.subDomain + '/order/delivery',
            header: {
			        'Content-Type': 'json'
			      },
            data: {
              token: wx.getStorageSync('token'),
              orderId: orderId
            },
            success: (res) => {
              if (res.data.code == 0) {
                that.onShow();
              }
            }
          })
        }
      }
    })
  },

  quanxiao:function (e){
    var that = this
    var order_id = that.data.orderId
    var order_num = that.data.ordernum
    var open_id = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var service = 'Pay.Refund';
    var client = 'web'
    var sign = ddapi.md5(client + open_id + order_id + order_num + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=Pay.Refund',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        user_id: user_id,
        openid: open_id,
        order_id: order_id,
        order_num: order_num,
        client: client
      },
      success: function (res) {
        console.log(res);
        if (res.data.data.code==0){
          wx.showToast({
            title: '退款申请成功',
          })
          setTimeout(function (){
            wx.navigateTo({
              url: "/pages/order-details/index?id=" + order_id + "&ordernum=" + order_num
            })
          }, 1000)
        }else{
          wx.showToast({
            title: res.data.data.msg,
            image: '../../../images/popup-close.png'
          })
        }
      }
    })
  },

  submitReputation: function (e) {
    var that = this;
    var postJsonString = {};
    postJsonString.token = wx.getStorageSync('token');
    postJsonString.orderId = this.data.orderId;
    var reputations = [];
    var i = 0;
    while (e.detail.value["orderGoodsId" + i]) {
      var orderGoodsId = e.detail.value["orderGoodsId" + i];
      var goodReputation = e.detail.value["goodReputation" + i];
      var goodReputationRemark = e.detail.value["goodReputationRemark" + i];

      var reputations_json = {};
      reputations_json.id = orderGoodsId;
      reputations_json.reputation = goodReputation;
      reputations_json.remark = goodReputationRemark;

      reputations.push(reputations_json);
      i++;
    }
    postJsonString.reputations = reputations;
    wx.showLoading();
    wx.request({
      url: 'https://ml002.zntech-china.com/' + app.globalData.subDomain + '/order/reputation',
      header: {
        'Content-Type': 'json'
      },
      data: {
        postJsonString: postJsonString
      },
      success: (res) => {
        wx.hideLoading();
        if (res.data.code == 0) {
          that.onShow();
        }
      }
    })
  },

  updateStatusSteps: function () {
    var that = this
    if (that.data.orderState === '1') {
      that.setData({
        statusSteps: [
          {
            current: true,
            done: false,
            text: '待支付',
            desc: '等待中...'
          },
          {
            current: false,
            done: false,
            text: '待使用',
            desc: ''
          },
          {
            current: false,
            done: false,
            text: '待评价',
            desc: ''
          },
          {
            current: false,
            done: false,
            text: '已完成',
            desc: ''
          }
        ]
      })
    } else if (that.data.orderState === '2') {
      that.setData({
        statusSteps: [
          {
            current: false,
            done: true,
            text: '待支付',
            desc: '成功'
          },
          {
            current: true,
            done: false,
            text: '待使用',
            desc: '等待中...'
          },
          {
            current: false,
            done: false,
            text: '待评价',
            desc: ''
          },
          {
            current: false,
            done: false,
            text: '已完成',
            desc: ''
          }
        ]
      })
    } else if (that.data.orderState === '3') {
      that.setData({
        statusSteps: [
          {
            current: false,
            done: true,
            text: '待支付',
            desc: '成功'
          },
          {
            current: false,
            done: true,
            text: '待使用',
            desc: '成功'
          },
          {
            current: true,
            done: false,
            text: '待评价',
            desc: '等待中...'
          },
          {
            current: false,
            done: false,
            text: '已完成',
            desc: ''
          }
        ]
      })
    } else if (that.data.orderState === '4') {
      that.setData({
        statusSteps: [
          {
            current: false,
            done: true,
            text: '待支付',
            desc: '成功'
          },
          {
            current: false,
            done: true,
            text: '待使用',
            desc: '成功'
          },
          {
            current: false,
            done: true,
            text: '待评价',
            desc: '成功'
          },
          {
            current: true,
            done: true,
            text: '已完成',
            desc: ''
          }
        ]
      })
    }
  },

  callPhone: function () {
    var that = this
    var phone = that.data.orderDetail[0].order_shop_tel
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function () {
        console.log("成功拨打电话")
      },
    })
  },

  callPhoneDistribution: function () {
    var that = this
    var phone = that.data.distribution_tel
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function () {
        console.log("成功拨打电话")
      },
    })
  },
  controltap: function (e){

    this.setData({
      latitude: this.data.latitude,
      longitude: this.data.longitude,
    })
    console.log(this.data);
    console.log('地图重置');
  },
})